﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using DTO;
namespace DAL
{
   public class LEVELDAL:KetNoiDuLieu
    {
        public void Insert(LEVEL lv)
        {
            SqlCommand cm = new SqlCommand("sp_level_Insert", LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MA",lv.Ma);
            cm.ExecuteNonQuery();
        }
        public void Update(LEVEL lv)
        {
            SqlCommand cm = new SqlCommand("sp_level_Update",LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MA", lv.Ma);
            cm.Parameters.AddWithValue("@USER_NAME",lv.Username);
            cm.Parameters.AddWithValue("@MA_TRANG", lv.Matrang);
            cm.Parameters.AddWithValue("@TRANG_THAI", lv.Trangthai);
            cm.ExecuteNonQuery();
        }
        public void Delete(LEVEL Ma)
        {
            SqlCommand cm = new SqlCommand("sp_level_Delete", LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MA",Ma);              
            cm.ExecuteNonQuery();
        }
        public LevelColection GET(string Where, int PageIndex, int PageSize, string OrderBy,
                                        string OrderDirection, out int TotalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_level_GET", LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;

            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", PageIndex);
            cm.Parameters.AddWithValue("@PageSize", PageSize);
            cm.Parameters.AddWithValue("@OrderBy", OrderBy);
            cm.Parameters.AddWithValue("@OrderDirection", OrderDirection);

            SqlParameter param = new SqlParameter("@TotalRecords", DbType.Int32);
            param.Direction = ParameterDirection.Output;
            cm.Parameters.Add(param);


            SqlDataAdapter da = new SqlDataAdapter(cm);
            DataSet ds = new DataSet();
            da.Fill(ds);

            LevelColection lvcl = new LevelColection();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                LEVEL lv = new LEVEL();
                lv.Ma = int.Parse(ds.Tables[0].Rows[i]["MA"].ToString());
                lv.Username = ds.Tables[0].Rows[i]["USER_NAME"].ToString();
                lv.Matrang = int.Parse(ds.Tables[0].Rows[i]["MA_TRANG"].ToString());
                lv.Trangthai = ds.Tables[0].Rows[i]["TRANG_THAI"].ToString();
                lvcl.Add(lv);

            }
            TotalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return lvcl;
        }


        public void Delete(int Ma)
        {
            throw new NotImplementedException();
        }
    }
}
